{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"MODEL_DIR\"] = '../model'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Better speed can be achieved with apex installed from https://www.github.com/nvidia/apex.\n",
      "--------------------\n",
      "Original Input:The quick brown fox jumps over the lazy dog\n",
      "Agumented Output:The quick U_RF F9F9F9F9F3W2TM jumps over the lazy dog\n",
      "--------------------\n",
      "Original Input:asdasd test apple dog asd asd\n",
      "Agumented Output:asdasd test apple dog asd 5hd\n"
     ]
    }
   ],
   "source": [
    "import sklearn.datasets\n",
    "import re\n",
    "\n",
    "import nlpaug.augmenter.word as naw\n",
    "import nlpaug.model.word_stats as nmw\n",
    "\n",
    "def _tokenizer(text, token_pattern=r\"(?u)\\b\\w\\w+\\b\"):\n",
    "    token_pattern = re.compile(token_pattern)\n",
    "    return token_pattern.findall(text)\n",
    "\n",
    "# Load sample data\n",
    "train_data = sklearn.datasets.fetch_20newsgroups(subset='train', remove=('headers', 'footers', 'quotes'))\n",
    "train_x = train_data.data\n",
    "\n",
    "# Tokenize input\n",
    "train_x_tokens = [_tokenizer(x) for x in train_x]\n",
    "\n",
    "# Train TF-IDF model\n",
    "tfidf_model = nmw.TfIdf()\n",
    "tfidf_model.train(train_x_tokens)\n",
    "tfidf_model.save('.')\n",
    "\n",
    "# Load TF-IDF augmenter\n",
    "aug = naw.TfIdfAug(model_path='.', tokenizer=_tokenizer)\n",
    "\n",
    "texts = [\n",
    "    'The quick brown fox jumps over the lazy dog',\n",
    "    'asdasd test apple dog asd asd'\n",
    "]\n",
    "\n",
    "for text in texts:\n",
    "    augmented_text = aug.augment(text)\n",
    "    \n",
    "    print('-'*20)\n",
    "    print('Original Input:{}'.format(text))\n",
    "    print('Agumented Output:{}'.format(augmented_text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
